<%-------------------------------------------------------------------
 * 页面名称：verifyImage.jsp
 * 功能描述：校验码页面
 * Action  :
 *
 *-------------------------------------------------------------------
--%>
<%@ page contentType="image/jpeg" %>
<%@ page import="javax.imageio.*" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.awt.*" %>

<%!
Color getRandColor(int fc,int bc){//
        Random random = new Random();
        if(fc>255) fc=255;
        if(bc>255) bc=255;
        int r=fc+random.nextInt(bc-fc);
        int g=fc+random.nextInt(bc-fc);
        int b=fc+random.nextInt(bc-fc);
        return new Color(r,g,b);
        }
%> 
<%
//éè??ò3??2??o′?
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);

 // 在内存中创建图象 
int width=58, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// 获取图形上下文
Graphics g = image.getGraphics();

 //生成随机类
Random random = new Random();

// 设定背景色 
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);

 //设定字体
g.setFont(new Font("Times New Roman",Font.BOLD,18));

//画边框 
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);


// 随机产生255条干扰线，使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
 int x = random.nextInt(width);
 int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12); 
 g.drawLine(x,y,x+xl,y+yl);
}

//获取校验码，校验码事先已被放入Session中,如果没有(可能是Session过期)，就创建一个
String key = cn.agiledata.bank.common.util.Constant.SESS_VERICODE ;
//String vericode = (String) session.getAttribute(key) ;
//if (vericode == null) {
	String vericode = cn.agiledata.bank.common.util.UniqueIdentityUtil.createRandomString(4) ;
	session.setAttribute(key, vericode) ;
//}

//将字符显示为图片
for (int i = 0; i < vericode.length(); i++){
    String rand = vericode.substring (i, i + 1) ;
    g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//μ÷ó?oˉêy3?à′μ???é??àí?￡??é?üê?òò?a??×óì??ó?ü￡??ùò????ü?±?óéú3é
    g.drawString(rand,12*i+5,15);
}

 // 图象生效
g.dispose();

 out.clearBuffer(); 
 out = pageContext.pushBody();

// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());%>
